package com.epmis.epmis.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.epmis.epmis.Service.ExamineService;
import com.epmis.epmis.Service.MissionService;
import com.epmis.epmis.Service.UserService;
import com.epmis.epmis.pojo.*;
import com.epmis.epmis.utils.ImageUitl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Controller
public class UserController {
    @Resource
    UserService userService;
    @Resource
    MissionService missionService;
    @Resource
    ExamineService examineService;

    @PostMapping("/getEnterPriseName")
    @ResponseBody
    public Enterprise getEnterPriseName(@RequestBody HashMap<String, Object> map) {
        return userService.getEnterpriseName((String) map.get("enterprise_id"));
    }

    @PostMapping("/getDepartmentName")
    @ResponseBody
    public Department getDepartmentName(@RequestBody HashMap<String, Object> map) {
        return userService.getDepartmentName(Integer.valueOf((String) map.get("department_id")));
    }

    @PostMapping("/getPostName")
    @ResponseBody
    public Post getPostName(@RequestBody HashMap<String, Object> map) {
        return userService.getPostName(Integer.valueOf((String) map.get("post_id")));
    }

    @PostMapping("/getUserReadNotification")
    @ResponseBody
    public List<Notification> getUserReadNotification(@RequestBody HashMap<String, Object> map) {
        return userService.getUserReadNotification(Integer.valueOf((String) map.get("recipient_id")));
    }

    @PostMapping("/getUserUnReadNotification")
    @ResponseBody
    public List<Notification> getUserUnReadNotification(@RequestBody HashMap<String, Object> map) {
        return userService.getUserUnReadNotification(Integer.valueOf((String) map.get("recipient_id")));
    }

    @PostMapping("/getMissionComment")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getMissionComment(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getMissionComment(pageCode, pageSize, id);
    }

    @PostMapping("/getExamineComment")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getExamineComment(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getExamineComment(pageCode, pageSize, id);
    }

    @PostMapping("/getMissionActivity")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getMissionActivity(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getMissionActivity(pageCode, pageSize, id);
    }

    @PostMapping("/getSonMission")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getSonMission(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getSonMission(pageCode, pageSize, id);
    }

    @PostMapping("/getRelMission")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getRelMission(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getRelMission(pageCode, pageSize, id);
    }

    @PostMapping("/getBeforeMission")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getBeforeMission(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getBeforeMission(pageCode, pageSize, id);
    }

    @PostMapping("/getAfterMission")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getAfterMission(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getAfterMission(pageCode, pageSize, id);
    }

    @PostMapping("/getMissionWork")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getMissionWork(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getMissionWork(pageCode, pageSize, id);
    }

    @PostMapping("/getMissionWorkTime")
    @ResponseBody
    public HashMap<String, Object> getMissionWorkTime(@RequestBody HashMap<String, Object> map) {
        int id = (int) map.get("id");
        return missionService.getMissionWorkTime(id);
    }

    @PostMapping("/getSelectSonMission")
    @ResponseBody
    public List<HashMap<String, Object>> getSelectSonMission(@RequestBody HashMap<String, Object> map) {
        int id = Integer.valueOf((String) map.get("id"));
        return missionService.getSelectSonMission(id);
    }

    @PostMapping("/getSelectRelMission")
    @ResponseBody
    public List<HashMap<String, Object>> getSelectRelMission(@RequestBody HashMap<String, Object> map) {
        int id = (int) map.get("id");
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.getSelectRelMission(id, enterpriseId);
    }

    @PostMapping("/getSelectBeforeMission")
    @ResponseBody
    public List<HashMap<String, Object>> getSelectBeforeMission(@RequestBody HashMap<String, Object> map) {
        int id = (int) map.get("id");
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.getSelectBeforeMission(id, enterpriseId);
    }

    @PostMapping("/getSelectAfterMission")
    @ResponseBody
    public List<HashMap<String, Object>> getSelectAfterMission(@RequestBody HashMap<String, Object> map) {
        int id = (int) map.get("id");
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.getSelectAfterMission(id, enterpriseId);
    }


    @PostMapping("/updateSonMission")
    @ResponseBody
    public int updateSonMission(@RequestBody HashMap<String, Object> map) {
        ArrayList list = (ArrayList) map.get("SonMissionSelect");
        int id = (int) map.get("id");
        return missionService.updateSonMission(list, id);
    }

    @PostMapping("/updateRelMission")
    @ResponseBody
    public int updateRelMission(@RequestBody HashMap<String, Object> map) {
        ArrayList list = (ArrayList) map.get("RelMissionSelect");
        int id = (int) map.get("id");
        return missionService.updateRelMission(list, id);
    }

    @PostMapping("/updateBeforeMission")
    @ResponseBody
    public int updateBeforeMission(@RequestBody HashMap<String, Object> map) {
        ArrayList list = (ArrayList) map.get("BeforeMissionSelect");
        int id = (int) map.get("id");
        return missionService.updateBeforeMission(list, id);
    }

    @PostMapping("/updateAfterMission")
    @ResponseBody
    public int updateAfterMission(@RequestBody HashMap<String, Object> map) {
        ArrayList list = (ArrayList) map.get("AfterMissionSelect");
        int id = (int) map.get("id");
        return missionService.updateAfterMission(list, id);
    }

    @PostMapping("/getMissionFile")
    @ResponseBody
    public PageInfo<HashMap<String, Object>> getMissionFile(@RequestBody HashMap<String, Object> map) {
        int pageCode = (int) map.get("pageCode");
        int pageSize = (int) map.get("pageSize");
        String id = String.valueOf((int) map.get("id"));
        return userService.getMissionFile(pageCode, pageSize, id);
    }

    @PostMapping("/uploadHead")
    @ResponseBody
    public FileResult uploadHead(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws FileNotFoundException {
        String path = ResourceUtils.getURL("classpath:").getPath() + "static/img/";
        String url = request.getContextPath() + "/img/";
        File filePath = new File(path);
        System.out.println("文件的保存路径：" + path);
        if (!filePath.exists() && !filePath.isDirectory()) {
            System.out.println("目录不存在，创建目录:" + filePath);
            filePath.mkdirs(); // mkdir()不会创建目录,找不到相应路径时返回false;而mkdirs()当目录不存在时则会创建相应目录
        }
        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        //获取文件类型，以最后一个`.`为标识
        String type = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();

        int ranNum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
        String fileName = str + ranNum + "." + type; // 新文件名，这里可以根据需要改名
        //在指定路径下创建一个文件
        File targetFile = new File(path, fileName); // 未使用outputStream.write()的时候,是一个File对象,保存在内存中,硬盘中看不到,但是可以使用这个对象
        try {
            // 使用springmvc的transferTo方法上传文件
            file.transferTo(targetFile);
            String msg = "data:image/jpeg;base64," + ImageUitl.ImageToBase64ByLocal(fileName);
            return new FileResult(true, msg, url + fileName, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new FileResult(false, "上传失败", "", "");
        }
    }

    @PostMapping("/uploadFile")
    @ResponseBody
    public FileResult uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws FileNotFoundException {

        String endpoint = "oss-cn-shenzhen.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5t5woVtxVbK6JdBWTdfD";
        String accessKeySecret = "T7QNkN3sSLU8YyTFq5JVfdcN7eYlpo";
// 填写Bucket名称。
        String bucketName = "zgtest123";

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        File files = null;
        try {
            String originalFilename = file.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            files = File.createTempFile(filename[0], filename[1]);
            file.transferTo(files);
            files.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
// 创建PutObjectRequest对象。
// 填写Bucket名称、Object完整路径和本地文件的完整路径。Object完整路径中不能包含Bucket名称。
// 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, file.getOriginalFilename(), files);
        ossClient.putObject(putObjectRequest);
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000 * 24);
        String url = ossClient.generatePresignedUrl(bucketName, file.getOriginalFilename(), expiration).toString();
        System.out.println(url);
        ossClient.shutdown();


        //获取原始文件名称(包含格式)
        String originalFileName = file.getOriginalFilename();
        long size = file.getSize();
        String[] sizeArray = new String[]{"byte", "KB", "MB", "GB"};//定义单位数组
        int i = 0;//选用第几个单位
        while (size >= 1024)//当当前值大于等于1024时，即转换到下一位，如此循环。当然也可设置为1000，或者更低
        {
            size = size / 1024;//当前值除以1024
            i++;//所选取的单位增加一个
        }
        String newSize = size + sizeArray[i];//最终结果为最终值加上单位
        //获取文件类型，以最后一个`.`为标识
        //在指定路径下创建一个文件
        try {
            // 使用springmvc的transferTo方法上传文件
            String msg = originalFileName;
            return new FileResult(true, msg, newSize, url, originalFileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new FileResult(false, "上传失败", "", "");
        }
    }

    @PostMapping("/createMissionFile")
    @ResponseBody
    public int createMissionFile(@RequestBody HashMap<String, Object> map) {
        return missionService.createMissionFile(map);
    }

    @PostMapping("/createExamineFile")
    @ResponseBody
    public int createExamineFile(@RequestBody HashMap<String, Object> map) {
        return missionService.createExamineFile(map);
    }

    @PostMapping("/updateHeadImg")
    @ResponseBody
    public int updateHeadImg(@RequestBody HashMap<String, Object> map) {
        String ImgName = (String) map.get("ImgName");
        int userId = Integer.valueOf((String) map.get("userId"));
        return userService.updateHeadImg(ImgName, userId);
    }

    @PostMapping("/updateUserName")
    @ResponseBody
    public int updateUserName(@RequestBody HashMap<String, Object> map) {
        String username = (String) map.get("username");
        int userId = Integer.valueOf((String) map.get("userId"));
        return userService.updateUserName(username, userId);
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public int updatePassword(@RequestBody HashMap<String, Object> map) {
        String newPassword = (String) map.get("newPassword");
        String oldPassword = (String) map.get("oldPassword");
        String username = (String) map.get("username");
        return userService.updatePassword(newPassword, oldPassword, username);
    }

    @PostMapping("/updatePhone")
    @ResponseBody
    public int updatePhone(@RequestBody HashMap<String, Object> map) {
        String phone = (String) map.get("phone");
        int userId = Integer.valueOf((String) map.get("userId"));
        return userService.updatePhone(phone, userId);
    }

    @PostMapping("/updateEmail")
    @ResponseBody
    public int updateEmail(@RequestBody HashMap<String, Object> map) {
        String email = (String) map.get("email");
        long createTime = (long) map.get("createTime");
        long spent = new Date().getTime() - createTime;
        int userId = Integer.valueOf((String) map.get("userId"));
        if (spent / (1000 * 60) > 5) {
            return 2;
        } else {
            return userService.updateEmail(email, userId);
        }
    }

    @PostMapping("/updateNotificationStatue")
    @ResponseBody
    public int updateNotificationStatue(@RequestBody HashMap<String, Object> map) {
        return userService.updateNotificationStatue((Integer) map.get("id"));
    }

    @PostMapping("/deleteSonMission")
    @ResponseBody
    public int deleteSonMission(@RequestBody HashMap<String, Object> map) {
        return missionService.deleteSonMission((int) map.get("id"));
    }

    @PostMapping("/deleteWorkTime")
    @ResponseBody
    public int deleteWorkTime(@RequestBody HashMap<String, Object> map) {
        return missionService.deleteWorkTime((int) map.get("id"));
    }

    @PostMapping("/deleteMissionFile")
    @ResponseBody
    public int deleteMissionFile(@RequestBody HashMap<String, Object> map) {
        return missionService.deleteMissionFile((int) map.get("id"));
    }

    @PostMapping("/deleteRelMission")
    @ResponseBody
    public int deleteRelMission(@RequestBody HashMap<String, Object> map) {
        return missionService.deleteRelMission((int) map.get("id"), (int) map.get("missionId"));
    }

    @PostMapping("/deleteBeforeMission")
    @ResponseBody
    public int deleteBeforeMission(@RequestBody HashMap<String, Object> map) {
        return missionService.deleteBeforeMission((int) map.get("id"), (int) map.get("missionId"));
    }

    @PostMapping("/updateMissionTitle")
    @ResponseBody
    public int updateMissionTitle(@RequestBody HashMap<String, Object> map) {
        String title = (String) map.get("title");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionTitle(title, id, userId, enterpriseId);
    }

    @PostMapping("/updateMissionStatus")
    @ResponseBody
    public int updateMissionStatus(@RequestBody HashMap<String, Object> map) {
        int status = (int) map.get("status");
        int id = (int) map.get("id");
        return missionService.updateMissionStatus(status, id);
    }

    @PostMapping("/updateMissionProjectStatus")
    @ResponseBody
    public int updateMissionProjectStatus(@RequestBody HashMap<String, Object> map) {
        String status = (String) map.get("status");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionProjectStatus(status, id, userId, enterpriseId);
    }

    @PostMapping("/updateTargetStatus")
    @ResponseBody
    public int updateTargetStatus(@RequestBody HashMap<String, Object> map) {
        String status = (String) map.get("status");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateTargetStatus(status, id, userId, enterpriseId);
    }

    @PostMapping("/updateCostStatus")
    @ResponseBody
    public int updateCostStatus(@RequestBody HashMap<String, Object> map) {
        String status = (String) map.get("status");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateCostStatus(status, id, userId, enterpriseId);
    }

    @PostMapping("/updateMissionPrincipal")
    @ResponseBody
    public int updateMissionPrincipal(@RequestBody HashMap<String, Object> map) {
        int principal_id = (int) map.get("principal_id");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionPrincipal(principal_id, id, userId, enterpriseId);
    }

    @PostMapping("/updateMissionStart")
    @ResponseBody
    public int updateMissionStart(@RequestBody HashMap<String, Object> map) throws ParseException {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start_date = date.parse((String) map.get("start_date"));
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionStart(start_date, id, userId, enterpriseId);
    }

    @PostMapping("/updateMissionOver")
    @ResponseBody
    public int updateMissionOver(@RequestBody HashMap<String, Object> map) throws ParseException {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date over_date = date.parse((String) map.get("over_date"));
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionOver(over_date, id, userId, enterpriseId);
    }

    @PostMapping("/updateMissionPhase")
    @ResponseBody
    public int updateMissionPhase(@RequestBody HashMap<String, Object> map) {
        String phase = (String) map.get("phase");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionPhase(phase, id, userId, enterpriseId);
    }

    @PostMapping("/updateMissionPriority")
    @ResponseBody
    public int updateMissionPriority(@RequestBody HashMap<String, Object> map) {
        String priority = (String) map.get("priority");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionPriority(priority, id, userId, enterpriseId);
    }

    @PostMapping("/updateMissionDescription")
    @ResponseBody
    public int updateMissionDescription(@RequestBody HashMap<String, Object> map) {
        String description = (String) map.get("description");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionDescription(description, id, userId, enterpriseId);
    }

    @PostMapping("/updateMissionParticipant")
    @ResponseBody
    public int updateMissionParticipant(@RequestBody HashMap<String, Object> map) {
        ArrayList list = (ArrayList) map.get("participant");
        String participant = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                participant = "," + list.get(i) + ",";
            } else {
                participant += list.get(i) + ",";
            }
        }
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateMissionParticipant(participant, id, userId, enterpriseId);
    }

    @PostMapping("/updateTargetUnit")
    @ResponseBody
    public int updateTargetUnit(@RequestBody HashMap<String, Object> map) {
        String unit = (String) map.get("unit");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateTargetUnit(unit, id, userId, enterpriseId);
    }

    @PostMapping("/updateTargetInitial")
    @ResponseBody
    public int updateTargetInitial(@RequestBody HashMap<String, Object> map) {
        String initial_value = (String) map.get("initial_value");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateTargetInitial(initial_value, id, userId, enterpriseId);
    }

    @PostMapping("/updateTargetTarget")
    @ResponseBody
    public int updateTargetTarget(@RequestBody HashMap<String, Object> map) {
        String target_value = (String) map.get("target_value");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateTargetTarget(target_value, id, userId, enterpriseId);
    }

    @PostMapping("/updateTargetCurrent")
    @ResponseBody
    public int updateTargetCurrent(@RequestBody HashMap<String, Object> map) {
        String current_value = (String) map.get("current_value");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateTargetCurrent(current_value, id, userId, enterpriseId);
    }

    @PostMapping("/updateTargetExplain")
    @ResponseBody
    public int updateTargetExplain(@RequestBody HashMap<String, Object> map) {
        String explain = (String) map.get("explain");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateTargetExplain(explain, id, userId, enterpriseId);
    }

    @PostMapping("/updateTargetFeedback")
    @ResponseBody
    public int updateTargetFeedback(@RequestBody HashMap<String, Object> map) {
        String feedback = (String) map.get("feedback");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateTargetFeedback(feedback, id, userId, enterpriseId);
    }

    @PostMapping("/updateTargetStandard")
    @ResponseBody
    public int updateTargetStandard(@RequestBody HashMap<String, Object> map) {
        String standard = (String) map.get("standard");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateTargetStandard(standard, id, userId, enterpriseId);
    }

    @PostMapping("/updateCostFinance")
    @ResponseBody
    public int updateCostFinance(@RequestBody HashMap<String, Object> map) {
        String finance = (String) map.get("finance");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateCostFinance(finance, id, userId, enterpriseId);
    }

    @PostMapping("/updateCostBudget")
    @ResponseBody
    public int updateCostBudget(@RequestBody HashMap<String, Object> map) {
        Double budget = Double.valueOf((String) map.get("budget"));
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateCostBudget(budget, id, userId, enterpriseId);
    }

    @PostMapping("/updateCostExecute")
    @ResponseBody
    public int updateCostExecute(@RequestBody HashMap<String, Object> map) {
        Double execute = Double.valueOf((String) map.get("execute"));
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateCostExecute(execute, id, userId, enterpriseId);
    }

    @PostMapping("/updateCostHappen")
    @ResponseBody
    public int updateCostHappen(@RequestBody HashMap<String, Object> map) throws ParseException {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date happen_date = date.parse((String) map.get("happen_date"));
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateCostHappen(happen_date, id, userId, enterpriseId);
    }

    @PostMapping("/updateCostPay")
    @ResponseBody
    public int updateCostPay(@RequestBody HashMap<String, Object> map) throws ParseException {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date payment_id = date.parse((String) map.get("payment_id"));
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateCostPay(payment_id, id, userId, enterpriseId);
    }

    @PostMapping("/updateCostInformation")
    @ResponseBody
    public int updateCostInformation(@RequestBody HashMap<String, Object> map) {
        String information = (String) map.get("information");
        int id = (int) map.get("id");
        int userId = Integer.valueOf((String) map.get("userId"));
        int enterpriseId = Integer.valueOf((String) map.get("enterpriseId"));
        return missionService.updateCostInformation(information, id, userId, enterpriseId);
    }

    @PostMapping("/updateProspect")
    @ResponseBody
    public int updateProspect(@RequestBody HashMap<String, Object> map) {
        int id = (int) map.get("id");
        Double prospect = Double.valueOf((String) map.get("prospect"));
        return missionService.updateProspect(id, prospect);
    }

    //获取企业全员姓名id和头像
    @PostMapping("/getMemberMessage")
    @ResponseBody
    public List<HashMap<String, Object>> getMemberMessage(@RequestBody HashMap<String, Object> map) {
        int enterprise_id = Integer.valueOf((String) map.get("enterprise_id"));
        List<User> user = userService.getMemberMessage(enterprise_id);
        List<HashMap<String, Object>> list = user.stream().map(res -> {
            HashMap<String, Object> newMap = new HashMap<>();
            newMap.put("value", res.getId());
            newMap.put("label", res.getName());
            try {
                newMap.put("circleUrl", "data:image/jpeg;base64," + ImageUitl.ImageToBase64ByLocal(res.getPhoto()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return newMap;
        }).collect(Collectors.toList());
        return list;
    }

    //创建项目
    @PostMapping("/createProject")
    @ResponseBody
    public int createProject(@RequestBody HashMap<String, Object> map) {
        return userService.createProject(map);
    }

    //创建目标
    @PostMapping("/createTarget")
    @ResponseBody
    public int createTarget(@RequestBody HashMap<String, Object> map) {
        return userService.createTarget(map);
    }

    //创建审批
    @PostMapping("/createExamine")
    @ResponseBody
    public int createExamine(@RequestBody HashMap<String, Object> map) {
        return userService.createExamine(map);
    }

    //创建费用
    @PostMapping("/createCost")
    @ResponseBody
    public int createCost(@RequestBody HashMap<String, Object> map) {
        return userService.createCost(map);
    }

    //创建项目任务
    @PostMapping("/createProjectMission")
    @ResponseBody
    public int createProjectMission(@RequestBody HashMap<String, Object> map) throws ParseException {
        return userService.createProjectMission(map);
    }

    //创建任务评论
    @PostMapping("/createMissionComment")
    @ResponseBody
    public int createMissionComment(@RequestBody HashMap<String, Object> map) {
        return missionService.createMissionComment(map);
    }

    //创建审批评论
    @PostMapping("/createExamineComment")
    @ResponseBody
    public int createExamineComment(@RequestBody HashMap<String, Object> map) {
        return examineService.createExamineComment(map);
    }

    //创建工时
    @PostMapping("/createWorkTime")
    @ResponseBody
    public int createWorkTime(@RequestBody HashMap<String, Object> map) throws ParseException {
        return missionService.createWorkTime(map);
    }


    //删除任务评论
    @PostMapping("/deleteMissionComment")
    @ResponseBody
    public int deleteMissionComment(@RequestBody HashMap<String, Object> map) {
        int id = (int) map.get("id");
        return missionService.deleteMissionComment(id);
    }

    //删除审批评论
    @PostMapping("/deleteExamineComment")
    @ResponseBody
    public int deleteExamineComment(@RequestBody HashMap<String, Object> map) {
        int id = (int) map.get("id");
        return examineService.deleteExamineComment(id);
    }

    //删除消息
    @PostMapping("/deleteMessage")
    @ResponseBody
    public int deleteMessage(@RequestBody HashMap<String, Object> map) {
        return userService.deleteMessage((Integer) map.get("id"));
    }

    //删除消息
    @PostMapping("/deleteMission")
    @ResponseBody
    public int deleteMission(@RequestBody HashMap<String, Object> map) {
        return missionService.deleteMission((Integer) map.get("id"));
    }

    //下载文件
    @RequestMapping("/download")
//    @ResponseBody
    public void downLoad(@RequestBody HashMap<String, Object> map, HttpServletResponse response, HttpServletRequest request) throws IOException {

        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "oss-cn-shenzhen.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5t5woVtxVbK6JdBWTdfD";
        String accessKeySecret = "T7QNkN3sSLU8YyTFq5JVfdcN7eYlpo";
// 填写Bucket名称。
        String bucketName = "zgtest123";
// 填写不包含Bucket名称在内的Object完整路径，例如testfolder/exampleobject.txt。
        String objectName = "zgtest123/1.png";

// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 下载Object到本地文件，并保存到指定的本地路径中。如果指定的本地文件存在会覆盖，不存在则新建。
// 如果未指定本地路径，则下载后的文件默认保存到示例程序所属项目对应本地路径中。
        ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File("D:\\1.png"));

// 关闭OSSClient。
        ossClient.shutdown();
    }

    @PostMapping("/getMainPageResult")
    @ResponseBody
    public MainPageResult getMainPageResult(@RequestBody HashMap<String, Object> map){
        int id = Integer.valueOf((String)map.get("id"));
        MainPageResult mainPageResult = userService.getMainPageResult(id);
        return mainPageResult;
    }

    @PostMapping("/getReportPageResult")
    @ResponseBody
    public ReportResult getReportPageResult(@RequestBody HashMap<String, Object> map){
        return userService.getReportPageResult(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/getMyDoingMission")
    @ResponseBody
    public List<HashMap<String,Object>> getMyDoingMission(@RequestBody HashMap<String, Object> map){
        return userService.getMyDoingMission(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/getMyPostponeMission")
    @ResponseBody
    public List<HashMap<String,Object>> getMyPostponeMission(@RequestBody HashMap<String, Object> map){
        return userService.getMyPostponeMission(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/getDepartment")
    @ResponseBody
    public List<HashMap<String,Object>> getDepartment(@RequestBody HashMap<String, Object> map){
        return userService.getDepartment(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/getPost")
    @ResponseBody
    public List<HashMap<String,Object>> getPost(@RequestBody HashMap<String, Object> map){
        return userService.getPost(Integer.valueOf((String)map.get("id")));
    }

    @PostMapping("/getMoveDepartment")
    @ResponseBody
    public List<HashMap<String,Object>> getMoveDepartment(@RequestBody HashMap<String, Object> map){
        return userService.getMoveDepartment(Integer.valueOf((String)map.get("id")));
    }
}

